package cn.com.callback
import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONArray
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wisdge.dataservice.Result;
import cn.com.elite.Util;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;


/**
 * 
 * @Title: autoGetAreaCode
 * @Description: DE21007案件大小类数据同步（市平台提供）
 * @author: QAH
 * @date 2020-06-23
 *
 */

public class autoGetTypeMessage implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(autoGetTypeMessage.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String paramString){

		        String logPrefix = "DE21007案件大小类数据同步（市平台提供）";
				String requestId = UUID.randomUUID().toString();
				String time = String.valueOf(System.currentTimeMillis() / 1000L);
				// 设置头文件
				Map<String, String> headMap = new HashMap<String, String>();
				headMap.put("Content-Type", "application/json");
				headMap.put("funCode", "DE21007");
				headMap.put("appId", getProperty.get("DE.appId"));
				headMap.put("appKey", getProperty.get("DE.appKey"));
				headMap.put("requestId", requestId);
				headMap.put("timestamp", time);
				headMap.put("encoding", "");
				
				JSONObject callJson = new JSONObject();
	
				//log.debug(logPrefix + "调用地址URL：" + getProperty.get("DE.URL"));
				//log.debug(logPrefix + "调用头文件信息:" + headMap.toString());
				//log.debug(logPrefix + "调用发送报文信息：" + callJson.toString());
				String reJson = Util.doHttpPostUseHead(getProperty.get("DE.URL"), callJson.toString(), "UTF-8", 10000, 0, headMap);
	
				JSONObject inObject = new JSONObject(reJson);
				String errorCode = KHUtil.GetJV(inObject, "errorCode");
				String errorMsg = KHUtil.GetJV(inObject, "errorMsg");
				String data = KHUtil.GetJV(inObject, "data");
				if(errorCode.equals("0")) {
					
									long rstartTime = System.currentTimeMillis();
									long rendTime = System.currentTimeMillis();
					
									try {
										JSONArray arrJsonArray = new JSONArray(data);
					
										int arrInt = arrJsonArray.length();
										
										//log.debug(logPrefix + "接入List数量:" + arrInt);
					
										for (int i = 0; i < arrInt; i++) {
					
				         JSONObject arrCustJson = arrJsonArray.getJSONObject(i);
						//区域编码信息
						String classCode = KHUtil.GetJV(arrCustJson, "classCode");
						String classId = KHUtil.GetJV(arrCustJson, "classId");
						String parentName = KHUtil.GetJV(arrCustJson, "parentName");
						String parentCode = KHUtil.GetJV(arrCustJson, "parentCode");
						String typeId = KHUtil.GetJV(arrCustJson, "typeId");
						String className = KHUtil.GetJV(arrCustJson, "className");
						String dbStatus = KHUtil.GetJV(arrCustJson, "dbStatus");
						String parentId = KHUtil.GetJV(arrCustJson, "parentId");
						def getSelectSQL = eliteBean.chaxun_sql("Select TOP 1 classId From SZWG_Type_Message Where classId = '" + classId + "'", "dataSource");
					
						if(getSelectSQL.size()==0) {
						String[] batchSQL = new String[1];
						batchSQL[0] = "INSERT INTO SZWG_Type_Message (classCode,classId,parentName,parentCode,typeId,className,dbStatus,parentId,createdtime) VALUES ('" + classCode + "', '" + classId + "', '" + parentName + "', '" + parentCode + "', '" + typeId + "', '" + className + "', '" + dbStatus + "', '" + parentId + "',GETDATE());";
						def dataresult = eliteBean.updateBatch(batchSQL, "dataSource");
						}
						else if(getSelectSQL.size()==1) {
						String[] batchSQL = new String[1];
						batchSQL[0] = "update SZWG_Type_Message set classCode='" + classCode + "',classId='" + classId + "',parentName='" + parentName + "',parentCode='" + parentCode + "',typeId='" + typeId + "',className='" + className + "',dbStatus='" + dbStatus + "',parentId='" + parentId + "',createdtime=GETDATE() Where classId = '" + classId + "';";
						def dataresult = eliteBean.updateBatch(batchSQL, "dataSource");
						}
				

	}
	
  }
  catch (JSONException e) {
	  rendTime = System.currentTimeMillis();
	  log.error(logPrefix + "处理失败(" + e.getMessage() + "),审核结果处理时长(毫秒):" + (rendTime - rstartTime));
  }
 }
}
}